<?php
/**
 * @package        OpenCart
 * @author        Daniel Kerr
 * @copyright    Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
 * @license        https://opensource.org/licenses/GPL-3.0
 * @link        https://www.opencart.com
 */

use Service\Exception\ApiErrCode;

/**
 * Controller class
 *
 * @property Document document
 * @property Loader load
 * @property Request request
 * @property Language language
 * @property Session session
 * @property \Cart\Customer customer
 * @property Response response
 * @property Url url
 * @property Url front_url
 * @property Config config
 * @property Log log
 * @property \Cart\User user
 * @property \Cart\Cart cart
 * @property \Cart\Currency currency
 */
abstract class Controller {
	protected $registry;

	public function __construct($registry) {
		$this->registry = $registry;
	}

	public function __get($key) {
		return $this->registry->get($key);
	}

	public function __set($key, $value) {
		$this->registry->set($key, $value);
	}

    /**
     * @deprecated - oc 原来的方法，命名不规范，不要使用
     *
     * @param null $json
     */
	public function json_output($json = null)
    {
        $this->responseJson($json);
	}

    /**
     * @param array $data
     */
    public function responseJson($data)
    {
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($data));
	}

    /**
     * @param array $data
     */
    public function responseOK($data)
    {
        $this->responseJson([
            'code' => ApiErrCode::API_CODE_OK,
        ] + $data);
    }

    public function jsonError($error, $code = ApiErrCode::UNKNOWN_ERROR)
    {
        $this->responseJson(compact('code', 'error'));
        $this->response->output();
        exit();
    }

    /**
     * @param string $route - 示例： user/user_group
     *
     *      参数只能是路由的格式，不能换成 Model::class 的形式
     *      因为 ModelExtensionDashboardActivity 的目录是 model/extension/dashboard/activity
     *      但是 ModelUserUserGroup              的目录是 model/user/user_group
     *      ModelUserUserGroup 转成 model_user_user_group 后
     *      无法判断应该换成 user/user_group 还是 user/user/group
     *
     * @return Model
     * @throws Exception
     */
    protected function loadModel($route)
    {
        $this->load->model($route);

        $model_namespace = "model_" . str_replace('/', '_', $route);

        return $this->$model_namespace;
    }

    /**
     * @param $key
     * @param null $default
     *
     * @return mixed
     */
    protected function requestGet($key, $default = null)
    {
        return isset($this->request->get[$key])
            ? $this->request->get[$key]
            : $default;
    }

    /**
     * @param $key
     * @param null $default
     *
     * @return mixed
     */
    protected function requestPost($key, $default = null)
    {
        return isset($this->request->post[$key])
            ? $this->request->post[$key]
            : $default;
    }

    /**
     * @param $key
     * @param null $default
     *
     * @return mixed
     */
    protected function requestAny($key, $default = null)
    {
        return $this->requestGet($key) ?: $this->requestPost($key, $default);
    }

    /**
     * @param $key
     * @param null $default
     *
     * @return mixed
     */
    protected function sessionData($key, $default = null)
    {
        return isset($this->session->data[$key])
            ? $this->session->data[$key]
            : $default;
    }

    protected function isPost()
    {
        return $this->isRequestMethod('POST');
    }

    /**
     * @param $method
     *
     * @return bool
     */
    protected function isRequestMethod($method)
    {
        return $this->request->server['REQUEST_METHOD'] === ucwords($method);
    }

    /**
     * @return \Cart\Customer
     */
    protected function cartCustomer()
    {
        return $this->customer;
    }

    /**
     * 客户登录界面的图片
     *
     * @return mixed
     * @throws Exception
     */
    protected function loginBackGroundImg()
    {
        return $this->config->get('config_login_background_image')
            ? image_resize($this->config->get('config_login_background_image'), 1903 , 600)
            : '/catalog/view/theme/'. $this->config->get('theme_default_directory') .'/image/bg@2x.png';
    }

    /**
     * 客户忘记密码界面的图片
     *
     * @return mixed
     * @throws Exception
     */
    protected function forgottenBackGroundImg()
    {
        return $this->config->get('config_forgotten_background_image')
            ? image_resize($this->config->get('config_forgotten_background_image'), 1903 , 600)
            : '/catalog/view/theme/'. $this->config->get('theme_default_directory') .'/image/bg@2x.png';
    }

    public function helper()
    {
        return \Utils\Helper::getSingleton();
    }
}
